package com.slunk.platforms;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.slunk.accessors.SRenderSpriteAccessor;

public class SSleepManager  extends SRenderSprite {
	private SAction sleepAction;
	private SAction wakeAction;
	private TweenManager parentTweenManager;
	private boolean isBackgroundDisplayed = false;
	private SConsciousState userState;
	SSleepManagerEvents eventHandler;
	Timer countdownTimer;
	
	public SSleepManager(Sprite sprite, TweenManager parentTweenManager, SSleepManagerEvents eventHandler) {
		super(sprite);
		this.eventHandler = eventHandler;
		this.sleepAction = new SAction(SRenderSpriteAccessor.FADE,new float[]{1.0f},2.0f);
		this.wakeAction = new SAction(SRenderSpriteAccessor.FADE,new float[]{0.0f},2.0f);
		this.parentTweenManager = parentTweenManager;
		userState = SConsciousState.AWAKE;
	}
	
	public boolean isBackgroundDisplayed() {
		return isBackgroundDisplayed;
	}
	
	/**
	 * Count down to when the sleeping animation should be triggered.
	 * @param seconds
	 */
	public void startCountdownWithTime(int seconds) { 
		countdownTimer = new Timer();
		countdownTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	System.out.print("beginDosing");
            	hardAsleep();
            }  
        }, seconds);
	}
	
	public boolean isAwake() {
		if (userState == SConsciousState.AWAKE) {
			return true;
		}
		return false;
	}
	
	public boolean isAsleep() {
		if (userState == SConsciousState.ASLEEP) {
			return true;
		}
		return false;
	}
	
	public boolean queryConsciousState(SConsciousState state) {
		if (userState == state) {
			return true;
		}
		return false;
	}
	
	private void stopActions() {
		parentTweenManager.killTarget(this);
	}
	
	
	public void hardAsleep() {
		stopActions();
		isBackgroundDisplayed = true;
		userState = SConsciousState.DOSING;
		this.run(sleepAction,onSleepFinished);
	}
	/**
	 * sleepFinishedCallback
	 */
	private final TweenCallback onSleepFinished = new TweenCallback() {
		@Override
		public void onEvent(int arg0, BaseTween<?> arg1) {
			userState = SConsciousState.ASLEEP;
			System.out.println("sleepFinished");
			eventHandler.onSleepAnimationFinished(null);
		}
	};
	
	public void hardAwake() {
		stopActions();
		userState = SConsciousState.WAKING;
		this.run(wakeAction,onWakeFinished);
	}
	
	/**
	 * wakeFinishedCallback
	 */
	private final TweenCallback onWakeFinished = new TweenCallback() {
		@Override
		public void onEvent(int arg0, BaseTween<?> arg1) {
			isBackgroundDisplayed = false;
			userState = SConsciousState.AWAKE;
			eventHandler.onAwakeAnimationFinished(null);
		}
	};
	
	public void run(SAction action, TweenCallback callback) {
		Tween.to(this, action.getType(), action.getDuration()).target(action.getArgs()).setCallback(callback).start(parentTweenManager);
	}
	
	public void drawObject(SpriteBatch batch) {
		super.drawObject(batch);
	}
	
	public void onEnter() {
		//do nothing right now
		super.onEnter();
	}
	
	public void onExit() {
		//do nothing right now
		super.onExit();
	}
}